var h5url = "http://echarge.mmcqing.com:23982";//测试
// var h5url = "http://echarge.mmcqing.com:22982"//正式
var h5api = "http://echarge.mmcqing.com:23980";//测试
// var h5api = 'http://172.18.251.123:22980';//秦波
// var h5api = "http://echarge.mmcqing.com:22980";//正式
var apiUrl = {
    list:h5api+"/stake/v2/list", //获取充电桩列表
    stationGet: h5api+"/station/v2/get", ///v1/station/route/{stationId}获取公交站点 车辆列表
    orderlist:h5api+ "/v1/api/order/msgs", //订单列表
    orderDetail: h5api+'/v1/api/order/detail',//订单详情
    resources:h5api+"/v3/community/h5/resources",
}
var reSize = function() {
    var deviceWidth = document.documentElement.clientWidth > 750 ? 750 : document.documentElement.clientWidth;
    document.documentElement.style.fontSize = (deviceWidth / 7.5) + 'px';
};
reSize();
window.onresize = reSize;
var u = navigator.userAgent,
    app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

var token = getCookie("TOKEN");
if (token == null) {
    sessionStorage.removeItem("token")
    sessionStorage.getItem("token");
} else {
    sessionStorage.setItem("token", token);
}

function setNavBar(data) {
    //设置导航按钮颜色
    if (isIOS) {
        window.webkit.messageHandlers.setNavBarAttribute.postMessage(data);
    } else if (isAndroid) {
        window.android.setNavBarAttribute(JSON.stringify(data));
    }
}
function closeView(data) { //关闭原声view窗口 n个 data为个数
    if (isIOS) {
        window.webkit.messageHandlers.closeWebBackToPage.postMessage(data);
    } else if (isAndroid) {
        window.android.closeWebBackToPage(data);
    }
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/"
        //这个很重要代表在那个层级下可以访问cookie
        //console.log(d)
}

function win_close(data) { //关闭原声view窗口 1个 data 为回调函数
    if (isIOS) {
        window.webkit.messageHandlers.closeWebPage.postMessage(data);
    } else if (isAndroid) {
        window.android.closeWebPage(data);
    }
}
//提示框/加载框
function showToast(data) {
    // if (isIOS) {
    //     window.webkit.messageHandlers.showToast.postMessage(data);
    // } else if (isAndroid) {
    //     window.android.showToast(JSON.stringify(data));
    // }
    console.log(data.title)
}
// 获取链接携带参数
function getParam(name) {
    var search = document.location.search;
    var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
    var matcher = pattern.exec(search);
    var items = null;
    if (null != matcher) {
        try {
            items = decodeURIComponent(decodeURIComponent(matcher[1]));
        } catch (e) {
            try {
                items = decodeURIComponent(matcher[1]);
            } catch (e) {
                items = matcher[1];
            }
        }
    }
    return items;
};

//获取定位
function getCurrentLocation() {
    if (isAndroid) {
        window.android.getCurrentLocation();
    } else if (isIOS) {
        window.webkit.messageHandlers.getCurrentLocation.postMessage(null);
    }
}
//token 失效
function logout() {
    if (isAndroid) {
        window.android.logOut();
    } else if (isIOS) {
        window.webkit.messageHandlers.logOut.postMessage(null);
    }
}
//获取cookie 内容
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    } else {
        return null;
    }
}
//打开app新view
function win_open(url, hiddenNav, useUIWeb) {
    if (useUIWeb) {

    } else {
        useUIWeb = false;
    }
    var urlshow = { url: url, hiddenNav: hiddenNav, useUIWeb: useUIWeb };
    urlshow = JSON.stringify(urlshow);
    window.location.href = url;
    // if (isIOS) {
    //     window.webkit.messageHandlers.openNewWebPage.postMessage(urlshow);
    // } else if (isAndroid) {
    //     window.android.openNewWebPage(urlshow);
    // }
}
//关闭原生view 窗口 n个 /data number
function closeView(data) {

    if (isIOS) {
        window.webkit.messageHandlers.closeWebBackToPage.postMessage(data);
    } else if (isAndroid) {
        window.android.closeWebBackToPage(data);
    }
}
//关闭原生view 窗口一个 data 回调函数名称字符串
function win_close(data) {
    if (isIOS) {
        window.webkit.messageHandlers.closeWebPage.postMessage(data);
    } else if (isAndroid) {
        window.android.closeWebPage(data);
    }
}
//input 被键盘遮挡解决
if (window.navigator.userAgent.indexOf('Android') > -1 || window.navigator.userAgent.indexOf('Adr') > -1) {
    window.addEventListener("resize", function() {
        if (document.activeElement.tagName == "INPUT" || document.activeElement.tagName == "TEXTAREA") {
            window.setTimeout(function() {
                document.activeElement.scrollIntoViewIfNeeded();
            }, 0);
        }
    })
}

function checkLocationPermissions() { //检查手机是否开启定位
    if (isAndroid) {
        window.android.checkLocationPermissions();
    } else if (isIOS) {
        window.webkit.messageHandlers.checkLocationPermissions.postMessage(null);
    }
}

function openBusRouteStation(data) { //打开路线 展示站点 页面
    if (isAndroid) {
        window.android.openBusRouteStation(JSON.stringify(data));
    } else if (isIOS) {
        window.webkit.messageHandlers.openBusRouteStations.postMessage(data);
    }
}
function openMapAutoNavi(data){
    if (isAndroid) {
        window.android.openMapAutoNavi(JSON.stringify(data));
    } else if (isIOS) {
        window.webkit.messageHandlers.openMapAutoNavi.postMessage(data);
    }
}
function openChargingPage(id){
    if (isAndroid) {
        window.android.openChargingPage(id);
    } else if (isIOS) {
        window.webkit.messageHandlers.openChargingPage.postMessage(id);
    }
}
var $ajax = {
    get: function(url, param, successcallback, errorcallback,finallycallback) {
        Vue.http({
            method: 'GET',
            url: url,
            params: param,
            headers: { token: token }
        }).then(function(resd) {
            var res = resd.body;
            // console.log(resd)
            if (res.code == 200) {
                successcallback(res)
            } else if (res.code == 401) {
                logout();
            } else {
                showToast({ title: res.message, type: 3 })
            }
        }, function(_error) {
            if (errorcallback && typeof errorcallback == 'function') {
                errorcallback(_error);
            } else {
                showToast({ title: "请求失败，请稍后重试!", type: 3 })
            }
        }).finally(function(){
            if (finallycallback && typeof finallycallback == 'function') {
                finallycallback(_error);
            } 
            
        })
    },
    post: function(url, param, successcallback, errorcallback,finallycallback) {
        Vue.http({
            method: 'POST',
            url: url,
            params: param,
            headers: { 
                'Authorization': token,
             }
        }).then(function(resd) {
            var res = resd.body;
            if (res.code == 200) {
                successcallback(res)
            } else if (res.code == 401) {
                logout();
            } else {
                showToast({ title: res.message, type: 3 })
            }
        }, function(_error) {
            if (errorcallback && typeof errorcallback == 'function') {
                errorcallback(_error);
            } else {
                showToast({ title: "请求失败，请稍后重试!", type: 3 })
            }
        }).finally(function(){
            if (finallycallback && typeof finallycallback == 'function') {
                finallycallback(_error);
            }            
        })
    },
}